package leetcode_200;

/**
 *@author 周杨
 *CompareVersionNumbers_165 比较两个版本号的大小
 *describe:用指针去遍历 注意边界情况 AC 92%
 *2018年6月7日 下午3:15:45
 */
public class CompareVersionNumbers_165 {

	public static void main(String[] args) {
		CompareVersionNumbers_165 test=new CompareVersionNumbers_165();
		System.out.println(test.compareVersion("0.1", "1.1"));
		System.out.println(test.compareVersion("1.0.1", "1"));
		System.out.println(test.compareVersion("7.5.2.4", "7.5.3"));
		System.out.println(test.compareVersion("1.0", "1"));

	}
	public int compareVersion(String version1, String version2) {
		int l1=0,e1=0,l2=0,e2=0;
		while(e1<version1.length()&&e2<version2.length()) {
			while(e1!=version1.length()&&version1.charAt(e1)!='.')
				++e1;
			int i1=Integer.parseInt(version1.substring(l1, e1));
			while(e2!=version2.length()&&version2.charAt(e2)!='.')
				++e2;
			int i2=Integer.parseInt(version2.substring(l2, e2));
			if(i1>i2)
        		return 1;
        	else if(i2>i1)
        		return -1;
        	else {
        		e1++;
        		l1=e1;
        		e2++;
        		l2=e2;
        	}
		}
		if(e1==version1.length()+1&&e2<version2.length()+1) {
			while(e2<version2.length()) {
				char now=version2.charAt(e2);
				if(now<='9'&&now>'0')//可能出现 1.0 和1 的比较
					return -1;
				++e2;
			}
			return 0;
		}
		else if(e2==version2.length()+1&&e1<version1.length()+1) {
			while(e1<version1.length()) {
				char now=version1.charAt(e1);
				if(now<='9'&&now>'0')
					return 1;
				++e1;
			}
			return 0;
		}
		return 0;
    }

}
